package com.hqyj.javaSpringBoot.modules.test.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hqyj.javaSpringBoot.modules.common.entiy.ResultEntity;
import com.hqyj.javaSpringBoot.modules.common.entiy.SearchBean;
import com.hqyj.javaSpringBoot.modules.test.dao.CityDao;
import com.hqyj.javaSpringBoot.modules.test.entity.City;
import com.hqyj.javaSpringBoot.modules.test.service.CityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;
import java.util.Optional;

@Service
public class CityServiceImpl implements CityService {

    @Autowired
    private CityDao cityDao;

    @Override
    @Transactional
    public ResultEntity<City> insertCity(City city) {
        cityDao.insertCity(city);
        return new  ResultEntity<City>
                (ResultEntity.ResultStatus.SUCCESS.code,"添加成功",city);
    }

    @Override
    @Transactional
    public ResultEntity<City> updateCity(City city) {
        cityDao.upadateCity(city);
        return new ResultEntity<>(ResultEntity.ResultStatus.SUCCESS.code,"修改成功",city);
    }

    @Override
    @Transactional
    public ResultEntity<City> deleteCityByCityId(int cityId) {
        cityDao.deleteCityByCityId(cityId);
        return new ResultEntity<>(ResultEntity.ResultStatus.SUCCESS.code,"删除成功");
    }

    @Override
    public City getCityByCityId(int cityId) {
        return cityDao.getCityByCityId(cityId);

    }

    @Override
    public List<City> getCitiesByCountryId(int countryId) {
        //如果查到的为空，则返回一个空的集合对象
        return Optional.ofNullable(cityDao.getCitiesByCountryId(countryId)).
                orElse(Collections.emptyList());

    }

    @Override
    public PageInfo<City> getCitiesBySearchBean(SearchBean searchBean) {
        searchBean.initSerchBean();
        PageHelper.startPage(searchBean.getCurrentPage(),searchBean.getPageSize());
        return new PageInfo<>(Optional.ofNullable(cityDao.getCitiesBySearchBean(searchBean)).orElse(
                Collections.emptyList()));
    }
}
